home *** CD-ROM | disk | FTP | other *** search
- /*
- file: main.cpp
- */
-
-
- #include <tchar.h>
- #include "..\ext_h\CP_EXT.h"
- #include "main.h"
-
-
- // global variables
-
- HINSTANCE gDLLInstance = NULL;
-
-
-
- // dll main entry point
- // we need this to initialize above global instance
-
- BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
- {
- if ( DLL_PROCESS_ATTACH == fdwReason )
- gDLLInstance = hDLLInst;
-
- return TRUE;
- }
-
-
- // externals main entry point
-
- LRESULT CALLBACK CP_ENTRYPOINT( SHORT option,
- CPEntryProcPtr entryPoint,
- LPARAM *lparam,
- HANDLE *globals )
- {
- CPParamBlock pb; // used for callback to CopyPaste
- LRESULT result = 0L; // result of call. 0 == no error
-
-
- // if option < 0 then this is a notification of CopyPaste
- // if option > 0 then this is a menu select call
- switch ( option )
- {
- case eInitExternal:
- {
- // load our menu and give it to CP to install
- HMENU hmenu = LoadMenu ( gDLLInstance, "DATE_UTILITIES" );
- *lparam = (LPARAM) hmenu;
- result = (NULL == hmenu) ? GetLastError() : 0L;
- }
- break;
-
- case eDeinitExternal:
- {
- // we are about to exit. receive our HMENU to destroy
- if ( ! DestroyMenu ( (HMENU) *lparam ) )
- result = GetLastError();
- }
- break;
-
- // this is our first menu entry ( see ext.date.rc )
- case eFirstOption: // insert current date ( short )
- {
- HGLOBAL hglobal = GetDateTimeHandle (
- DATE_SHORTDATE, GetDateFormat );
- if ( hglobal )
- {
- pb.wparam1 = getCurrentClipboard(entryPoint);
- pb.wparam2 = CF_TEXT; // clipboard format
- pb.handle1 = hglobal; // data handle
- result = CallCP ( eSetClipboardItem, &pb );
- }
- // if everything works fine, the global handle
- // belongs to the clipboard.
- // On fail, you are responsible to free memory.
- if ( result )
- GlobalFree ( hglobal );
- }
- break;
-
- // this is our second menu entry ( see ext.date.rc )
- case 2: // insert current date ( long )
- {
- HGLOBAL hglobal = GetDateTimeHandle (
- DATE_LONGDATE, GetDateFormat );
- if ( hglobal )
- {
- pb.wparam1 = getCurrentClipboard(entryPoint);
- pb.wparam2 = CF_TEXT; // clipboard format
- pb.handle1 = hglobal; // data handle
- result = CallCP ( eSetClipboardItem, &pb );
- }
- if ( result )
- GlobalFree ( hglobal );
- }
- break;
-
- // this is our third menu entry ( see ext.date.rc )
- case 3: // insert current time
- {
- HGLOBAL hglobal = GetDateTimeHandle (
- TIME_FORCE24HOURFORMAT, GetTimeFormat );
- if ( hglobal )
- {
- pb.wparam1 = getCurrentClipboard(entryPoint);
- pb.wparam2 = CF_TEXT; // clipboard format
- pb.handle1 = hglobal; // data handle
- result = CallCP ( eSetClipboardItem, &pb );
- }
- if ( result )
- GlobalFree ( hglobal );
- }
- break;
- }
- return result;
- }
-
-
- // this one returns the currently selected clipboard
- int getCurrentClipboard (CPEntryProcPtr entryPoint)
- {
- CPParamBlock pb;
- int result;
-
- ZeroMemory ( &pb, sizeof(CPParamBlock) );
- result = CallCP ( eGetCurrentClipboard, &pb );
- return ( ! result ) ? pb.wparam1 : 0;
- }
-
-
- // this routine returns a formatted date/time in a globals handle
-
- HGLOBAL GetDateTimeHandle ( DWORD flags, DateTimeFormatFP dtfp )
- {
- SYSTEMTIME systemTime;
- TCHAR szBuffer[128];
- int numBytes;
- HGLOBAL hglobal = NULL;
-
- GetSystemTime(&systemTime);
-
- // first call to GetDateFormat() with cch == 0
- // to get the needed size
- numBytes = (*dtfp)( LOCALE_USER_DEFAULT, flags,
- &systemTime, NULL, (LPTSTR) szBuffer, 0 );
-
- // next call with cch == sizeof(szBuffer) to get
- // the real data.
- if ( numBytes > 0 )
- numBytes = (*dtfp)( LOCALE_USER_DEFAULT, flags,
- &systemTime, NULL, (LPTSTR) szBuffer, sizeof(szBuffer) );
-
- // if there are still bytes in our stack buffer, allocate
- // global memory and return as function result.
- if ( numBytes > 0 )
- {
- numBytes += sizeof(TCHAR);
- hglobal = GlobalAlloc ( GMEM_MOVEABLE | GMEM_DDESHARE, numBytes );
- if ( NULL != hglobal )
- {
- CopyMemory ( GlobalLock ( hglobal ), szBuffer, numBytes );
- GlobalUnlock ( hglobal );
- }
- }
- return hglobal;
- }
-